#pragma once
#include "afxcmn.h"
#include "afxwin.h"

#include "Utils/FastBlockQueue.h"
#include "Business/QueueContext.h"
#include "Business/Policy/ArbitrageOffsetBusiness.h"

typedef ArbitrageOffsetBusiness::Argument BusinessArgument;
typedef ArbitrageOffsetBusiness::Snapshot BusinessSnapshot;

class ThreadManager;
struct Command;

class CArbitrageOffsetBusinessDlg : public CDialog
{
	DECLARE_DYNAMIC(CArbitrageOffsetBusinessDlg)

public:
	CArbitrageOffsetBusinessDlg(ThreadManager& threadManager);   // standard constructor
	virtual ~CArbitrageOffsetBusinessDlg();

// Dialog Data
	enum { IDD = IDD_ARBITRAGE_OFFSET_BUSINESS_DLG };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	virtual BOOL OnInitDialog();

	afx_msg void OnBnClickedBtnAdd();
	afx_msg void OnBnClickedBtnModify();
	afx_msg void OnCbnSelchangeMajorCombo();
	afx_msg void OnNMClickBusinessList(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnLvnKeydownBusinessList(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnMove(int x, int y);
	afx_msg void OnTimer(UINT_PTR nIDEvent);

	DECLARE_MESSAGE_MAP()

	CListCtrl m_businessList;
	CComboBox m_majorCombo;
	CComboBox m_minorCombo;
	CComboBox m_offsetTypeCombo;
	CEdit m_quantityEdit;
	CEdit m_openBalanceEdit;
	CEdit m_offsetBalanceEdit;
	CEdit m_stopBalanceEdit;
private:
	void InitBusinessList();
	void InitContractCombo();
	void InitMinorCombo(UINT majorContract);
	void UpdateControls(BusinessArgument const& argument);

	bool IsOffsetToday() const;

	void UpdateContractCombo(CComboBox& combo,int contract);
	int GetBusinessIndex(ArbitrageOffsetBusiness* business) const;
	void OnCommand(Command const& cmd);
	bool Validate(BusinessArgument const& argument, ArbitrageOffsetBusiness const& business) const;
	BusinessArgument GetArgument();
	void HandlerShortcuts(int index, WORD key);
	int GetCommandType(WORD key);
	void UpdateBusiness(int index,BusinessArgument const& argument);
	void InsertBusiness(ArbitrageOffsetBusiness* business);
	ArbitrageOffsetBusiness* AddBusiness(BusinessArgument const& argument);


	ThreadManager& m_threadManager;
	FastBlockQueue::FastBlockQueuePtr m_uiQueue;
	QueueContext m_queueContext;
	ContractCodes const& m_contracts;
	std::map<ArbitrageOffsetBusiness*,int> m_businessIndexMap;

	int m_notifyColumnPosition;
protected:
	virtual void OnOK();
	virtual void OnCancel();
};
